package io.github.uicdb.badskill.clent;

import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import io.github.uicdb.badskill.BadSkill;
import lombok.Getter;
import lombok.Setter;
import net.minecraft.client.Minecraft;

import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.narration.NarratedElementType;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class HorScrollTabLayout extends AbstractWidget {
    final static ResourceLocation UNKNOWN_ICON=new ResourceLocation(BadSkill.MODID,"textures/gui/unknown_icon.png");
    final static ResourceLocation LAYOUT_BACKGROUND=new ResourceLocation(BadSkill.MODID,"textures/gui/tab_background.png");
    public final List<HsTab> tabList=new ArrayList<>();
    protected int scrollX;
    protected int consumedX=2;
    @Setter@Getter
    protected int padding=4;

    public HorScrollTabLayout(int pX, int pY, int pWidth, int pHeight, Component pMessage) {
        super(pX, pY, pWidth, pHeight, pMessage);

    }
    public void addTab(HsTab tab){
        tabList.add(tab);
        tab.setX(consumedX);
        tab.setY((height-tab.getHeight())/2);
        consumedX+=tab.getWidth()+padding;
    }


    @Override
    protected void updateWidgetNarration(NarrationElementOutput p_259858_) {

    }


    protected void renderBg(@NotNull GuiGraphics pPoseStack, @NotNull Minecraft pMinecraft, int pMouseX, int pMouseY) {
        pPoseStack.blit(LAYOUT_BACKGROUND,getX(),getY(),width,height,0,0,width,height);
    }
    boolean drag=false;



    @Override
    public boolean mouseClicked(double pMouseX, double pMouseY, int pButton) {
        if(isMouseOver(pMouseX,pMouseY)&&!drag) {
            for (HsTab tab : tabList) {
                var xx = tab.getX() - scrollX+getX();
                var yy = tab.getY()+getY();
                if (pMouseX > xx & pMouseX < xx + tab.getWidth() && pMouseY > yy & pMouseY < yy + tab.getHeight()) {
                    tab.getOnClick().onClick(tab.getPage());
                    return true;
                }
            }
        }
        return false;
    }
    public void scrollToRight(){
        if (consumedX>width)
            scrollTo(consumedX-width);
    }
    protected boolean canScroll(){
        return consumedX>width;
    }
    public void scrollToLeft(){
        scrollTo(0);
    }
    public void scrollTo(int add){
        scrollX= Mth.clamp(-add+scrollX,0,consumedX-width);
    }
    @Override
    public boolean mouseDragged(double pMouseX, double pMouseY, int pButton, double pDragX, double pDragY) {
        if(canScroll()) {
            drag = true;
            scrollX = (int) Mth.clamp(-pDragX + scrollX, 0, consumedX - width);
            return true;
        }
        return false;
    }

    @Override
    public boolean mouseReleased(double pMouseX, double pMouseY, int pButton) {
        if(drag){
            drag=false;
            return true;
        }
        return false;
    }

    @Override
    public boolean mouseScrolled(double pMouseX, double pMouseY, double pDelta) {
        if(isMouseOver(pMouseX, pMouseY)&canScroll()){
            int newOffset = scrollX + (int) (-pDelta * 20);
            scrollX= Mth.clamp(newOffset,0,consumedX-width);
            return true;
        }
        return false;
    }

    @Override
    public void renderWidget(@NotNull GuiGraphics pPoseStack, int pMouseX, int pMouseY, float pPartialTick) {
        RenderSystem.setShaderColor(1,1,1,1);
        renderBg(pPoseStack,Minecraft.getInstance(),pMouseX,pMouseY);
        RenderSystem.enableBlend();
        RenderSystem.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);
        pPoseStack.enableScissor(getX(),getY(),getX()+width,getY()+height);
        tabList.forEach(hsTab -> {
            if(hsTab.getIcon()!=null)
            pPoseStack.blit(hsTab.getIcon(),  getX()+hsTab.getX()-scrollX,getY()+hsTab.getY(),hsTab.getWidth(),hsTab.getHeight(),0,0 ,hsTab.getWidth(), hsTab.getHeight(),hsTab.getWidth(), hsTab.getHeight());
            else pPoseStack.blit(UNKNOWN_ICON,  getX()+hsTab.getX()-scrollX,getY()+hsTab.getY(),hsTab.getWidth(),hsTab.getHeight(),0,0 ,hsTab.getWidth(), hsTab.getHeight(),hsTab.getWidth(), hsTab.getHeight());

        });
        pPoseStack.disableScissor();
        RenderSystem.disableBlend();
    }
}
